package vn.huync.smsbackupandrestore;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.nch.smsbackupandrestore.R;

public class SetupActivity extends Activity {
	
	CheckBox cbSyncOnStartup;
	CheckBox cbSyncAfterBackup;
	RadioButton rbSaveToSdcard;
	RadioButton rbSaveToInternal;
	
	MyApp myApp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup);
        
        cbSyncOnStartup = (CheckBox)findViewById(R.id.cb_sync_on_startup);
        cbSyncAfterBackup = (CheckBox)findViewById(R.id.cb_sync_after_backup);
        rbSaveToSdcard = (RadioButton)findViewById(R.id.rb_save_sdcard);
        rbSaveToInternal = (RadioButton)findViewById(R.id.rb_save_internal);
        
        myApp = (MyApp)getApplicationContext();
        
        final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
        cbSyncOnStartup.setChecked(pref.getBoolean("c_sync_on_startup", true));
        cbSyncAfterBackup.setChecked(pref.getBoolean("c_sync_after_backup", true));
        
        boolean isSaveToSd = pref.getBoolean("is_save_sdcard", true);
        rbSaveToSdcard.setChecked(isSaveToSd);
        rbSaveToInternal.setChecked(!isSaveToSd);
   
        
        cbSyncOnStartup.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				Editor editor = pref.edit();
				editor.putBoolean("c_sync_on_startup", isChecked);
				editor.commit();
				
			}
		});
        
        cbSyncAfterBackup.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				Editor editor = pref.edit();
				editor.putBoolean("c_sync_after_backup", isChecked);
				editor.commit();
				
			}
		});
        
        rbSaveToSdcard.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				Editor editor = pref.edit();
				editor.putBoolean("is_save_sdcard", isChecked);
				editor.commit();
			}
		});
        
        boolean isSDPresent = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
        if(isSDPresent == false){
        	rbSaveToSdcard.setVisibility(View.GONE);
        }
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	if(rbSaveToSdcard.getVisibility() != View.VISIBLE){
    		rbSaveToInternal.setChecked(true);
    	}
    }
}
